/*
 * @Author: cl
 * @Description: 异常拦截器
 * @Date: 2023-04-11 11:34:20
 * @LastEditTime: 2023-04-11 17:04:14
 * @FilePath: \jsnode\nestjs\naota\src\common\filter.ts
 * @LastEditors: cl
 */

import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common'

@Catch(HttpException)
export class Filter<T = any> implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp()
    const req = ctx.getRequest()
    const response = ctx.getResponse()
    const status = exception.getStatus()
    response.status(status).json({
      status,
      success: false,
      time: new Date(),
      data: exception,
      path: req.url,
    })
  }
}
